package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.bac.BacExperimentDto;
import com.ysd.lis.entity.bac.BacExperiment;
import com.ysd.lis.mapper.bac.BacExperimentMapper;
import com.ysd.lis.service.bac.BacExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-04-07
 */
@Service
public class BacExperimentServiceImpl extends ServiceImpl<BacExperimentMapper, BacExperiment> implements BacExperimentService {

    @Autowired
    BacExperimentMapper bacExperimentMapper;

    @Override
    public Result findBacExperimentList(BacExperiment bacUnionColoCount) {
        List<BacExperiment> list = findBacExperimentArray(bacUnionColoCount);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacExperiment> findBacExperimentArray(BacExperiment bacExperiment) {
        MPJLambdaWrapper<BacExperiment> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacExperiment::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(bacExperiment.getInputCode()), BacExperiment::getInputCode, bacExperiment.getInputCode());
        queryWrapper.eq(StringUtils.isNotBlank(bacExperiment.getCode()), BacExperiment::getCode, bacExperiment.getCode());
        queryWrapper.eq(StringUtils.isNotBlank(bacExperiment.getName()), BacExperiment::getName, bacExperiment.getName());
        // if (StringUtils.isNotBlank(bacExperiment.getFilter())) {
        //     queryWrapper.and(query -> query.like(BacExperiment::getCode, bacExperiment.getFilter()).or().like(BacExperiment::getName, bacExperiment.getFilter()));
        // }
        queryWrapper.orderByAsc(BacExperiment::getSeq);
        return bacExperimentMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacExperiment(BacExperiment bacExperiment) {
        int i = bacExperimentMapper.insert(bacExperiment);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacExperiment);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacExperiment(BacExperiment bacExperiment) {
        bacExperiment.setDelFlag(1);
        int i = bacExperimentMapper.updateById(bacExperiment);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacExperiment);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacExperiment(BacExperiment bacExperiment) {
        int i = bacExperimentMapper.updateById(bacExperiment);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacExperiment);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result deleteBatchBacExperiment(BacExperimentDto experimentDto) {
        List<BacExperiment> bacExperimentList = experimentDto.getBacExperimentList();
        //收集id
        List<String> ids = bacExperimentList.stream().map(BacExperiment::getId).collect(java.util.stream.Collectors.toList());
        //根据id批量删除
        int i = bacExperimentMapper.deleteBatchIds(ids);
        if (i > 0) {
            return Result.succ(1, "批量删除成功", bacExperimentList);
        }else {
            return Result.fail("批量删除失败");
        }
    }


}
